package boogui.member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;

public class MemberDAO implements IMemberDAO {
	IBooguiDAO bdao = null;
	MemberSQL msql = null;

	public MemberDAO() {
		bdao = new BooguiDAO();
		msql = new MemberSQL();
	}

	// 회원 가입
	@Override
	public boolean insertMember(MemberVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getInsertMemberSQL());			
			pstmt.setString(1, m.getMemid());
			pstmt.setString(2, m.getMempwd());
			pstmt.setString(3, m.getMemname());
			pstmt.setString(4, m.getMembirth());
			pstmt.setString(5, m.getMemgenre());
			pstmt.setString(6, m.getMemhp());
			pstmt.setString(7, m.getMememail());
			pstmt.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}
		return result;
	}

	// 회원 정보 조회 (전체)
	@Override
	public List<MemberVO> selectMember() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MemberVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getSelectMemberSQL());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				MemberVO m = new MemberVO();
				m.setMemno(rs.getString("memno"));
				m.setMemid(rs.getString("memid"));
				m.setMempwd(rs.getString("mempwd"));
				m.setMemname(rs.getString("memname"));
				m.setMembirth(rs.getString("membirth"));
				m.setMemgenre(rs.getString("memgenre"));
				m.setMemhp(rs.getString("memhp"));
				m.setMememail(rs.getString("mememail"));
				result.add(m);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}

		return result;
	}		
		

	// 회원 정보 수정
	@Override
	public boolean updateMember(MemberVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getUpdateMemberSQL());
			pstmt.setString(1, m.getMempwd());
			pstmt.setString(2, m.getMemname());
			pstmt.setString(3, m.getMembirth());
			pstmt.setString(4, m.getMemgenre());
			pstmt.setString(5, m.getMemhp());
			pstmt.setString(6, m.getMememail());
			pstmt.setString(7, m.getMemid());
			pstmt.executeUpdate();
			result = true;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}

		return result;
	}

	// 회원 탈퇴
	@Override
	public boolean deleteMember(MemberVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getDeleteMemberSQL());

			pstmt.setString(1, m.getMemno());

			int cnt = pstmt.executeUpdate();
			if (cnt == 0)
				System.out.println("명령을 잘 실행 됬지만 수정된 데이터는 없음");
			result = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}

		return result;
	}
}


